package com.example.demo.utils.Date;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.DayOfWeek;
import java.time.LocalDate;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.time.temporal.TemporalField;
import java.time.temporal.WeekFields;
import java.util.*;

public class DateChartUtils {

    /**
     * 以当前时间计算增加减少天数
     * day传正数为增加
     * day传负数为减小
     */
    public static List<String> days(int day,String date){
        List list = new ArrayList();
        LocalDate now = LocalDate.parse(date);
        if(day == 0){
            String format = now.format(DateTimeFormatter.ofPattern("yyyy-MM-dd"));
            list.add(format);
            return list;
        }
        if(day < 0){
            for(int i = 0 ; i >= day ; i--){
                day(now,i,list);
            }
        }else{
            for(int i = 0 ; i <= day ; i++){
                day(now,i,list);
            }
        }
        return list;
    }


    public static List<Map<String,Object>> daysNew(int day,String date){
        List<Map<String,Object>> mapList = new ArrayList();
        LocalDate now = LocalDate.parse(date);
        if(day < 0){
            for(int i = 0 ; i > day ; i--){
                mapList.add(dayNew(now,i));
            }
            Collections.reverse(mapList);
        }else{
            for(int i = 0 ; i < day ; i++){
                mapList.add(dayNew(now,i));
            }
        }
        return mapList;
    }
    


    /**
     * 以当前时间计算增加减少月份
     * month传正数为增加
     * month传负数为减小
     */
    public static List<String> months(int month,String date){
        List list = new ArrayList();
        LocalDate now = LocalDate.parse(date);
        if(month == 0){
            String format = now.format(DateTimeFormatter.ofPattern("yyyy-MM"));
            list.add(format);
            return list;
        }
        if(month < 0){
            for(int i = 0 ; i >= month ; i--){
                month(now,i,list);
            }
        }else{
            for(int i = 0 ; i <= month ; i++){
                month(now,i,list);
            }
        }
        return list;
    }


    public static List<Map<String,Object>> monthsNew(int month,String date){
        List<Map<String,Object>> mapList = new ArrayList();
        LocalDate now = LocalDate.parse(date);
        if(month < 0){
            for(int i = 0 ; i > month ; i--){
                mapList.add(monthNew(now,i));
            }
            Collections.reverse(mapList);
        }else{
            for(int i = 0 ; i < month ; i++){
                mapList.add(monthNew(now,i));
            }
        }
        return mapList;
    }

    /**
     * 以当前时间计算增加减少月份
     * month传正数为增加
     * month传负数为减小
     */
    public static List<Map<String,Object>> weeks(int week,String date){
        List<Map<String,Object>> list = new ArrayList();
        LocalDate now = LocalDate.parse(date);
        if(week < 0){
            for(int i = 0 ; i > week ; i--){
                list.add(week(now, i));
            }
            Collections.reverse(list);
        }else{
            for(int i = 0 ; i < week ; i++){
                list.add(week(now, i));
            }
        }
        return list;
    }

    public static Map week(LocalDate now,int i){
        Map map = new HashMap();
        LocalDate localDate = now.plusWeeks(i);
        TemporalField fieldISO = WeekFields.of(DayOfWeek.MONDAY,1).dayOfWeek();
        //SUNDAY,1
        LocalDate with = localDate.with(fieldISO,1);
        String startTime = with.format(DateTimeFormatter.ofPattern("yyyy-MM-dd"));



        LocalDate with1 = localDate.with(fieldISO,7);
        String endTime = with1.format(DateTimeFormatter.ofPattern("yyyy-MM-dd"));


        map.put("startTime",startTime);
        map.put("endTime",endTime);


        String today = startTime;
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        Date date = null;
        try {
            date = format.parse(today);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        Calendar calendar = Calendar.getInstance();
        calendar.setFirstDayOfWeek(Calendar.MONDAY);
        calendar.setTime(date);
        String substring = endTime.substring(0, 4);
        map.put("youneed",substring+"年第"+calendar.get(Calendar.WEEK_OF_YEAR)+"周");
        return map;
    }

    public static void month(LocalDate now,int i,List list){
        LocalDate localDate = now.plusMonths(i);
        String format = localDate.format(DateTimeFormatter.ofPattern("yyyy-MM"));
        list.add(format);
    }

    public static void day(LocalDate now,int i,List list){
        LocalDate localDate = now.plusDays(i);
        String format = localDate.format(DateTimeFormatter.ofPattern("yyyy-MM-dd"));
        list.add(format);
    }

    public static Map<String,Object> monthNew(LocalDate now,int i){
        HashMap<String, Object> map = new HashMap<>();
        LocalDate localDate = now.plusMonths(i);
        String startTime = localDate.format(DateTimeFormatter.ofPattern("yyyy-MM"));


        Calendar calendar = Calendar.getInstance();
        ZoneId zoneId = ZoneId.systemDefault();
        ZonedDateTime zdt = localDate.atStartOfDay(zoneId);
        Date date = Date.from(zdt.toInstant());
        // 设置时间,当前时间不用设置
        calendar.setTime(date);
        // 设置日期为本月最大日期
        calendar.set(Calendar.DATE, calendar.getActualMaximum(Calendar.DATE));
        DateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        String endTime = format.format(calendar.getTime());
        map.put("startTime",startTime+"-01");
        map.put("endTime",endTime);
        return map;
    }

    public static Map<String,Object> dayNew(LocalDate now,int i){
        HashMap<String, Object> map = new HashMap<>();
        LocalDate localDate = now.plusDays(i);
        String format = localDate.format(DateTimeFormatter.ofPattern("yyyy-MM-dd"));
        map.put("startTime",format);
        map.put("endTime",format);
        return map;
    }

    public static void main(String[] args) {
    	String todayDate = DateUtils.parseDate(new Date(), DateUtils.YYYY_MM_DD); // 今日
        List days = DateChartUtils.days(0, todayDate);
        System.out.println(days);

//        List<Map<String, Object>> weeks = DateChartUtils.weeks(-2,"2018-12-31");
//        System.out.println(weeks);

//        List months = DateChartUtils.monthsNew(0, "2018-12-13");
//        System.out.println(months);
//    2018-12-12,2018-12-11,2018-12-10,2018-12-09,2018-12-08,2018-12-07
//    2018-12,2018-11,2018-10,2018-09,2018-08,2018-07,2018-06,2018-05,2018-04,2018-03,2018-02,2018-01
//    [{startTime=2018-12-10, endTime=2018-12-16}, {startTime=2018-12-03, endTime=2018-12-09}]

    }


//    public static void main(String[] args){
//
//
//
//
//        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd"); //设置时间格式
//        Calendar cal = Calendar.getInstance();
//        cal.setFirstDayOfWeek(Calendar.MONDAY);
//        Date time= null;
//        try {
//            time = sdf.parse("2018-11-25");
//        } catch (ParseException e) {
//            e.printStackTrace();
//        }
//        cal.setTime(time);
//        System.out.println("要计算日期为:"+sdf.format(cal.getTime())); //输出要计算日期
//
////n为推迟的周数，1本周，-1向前推迟一周，2下周，依次类推
//        int n = 0;
//        String monday;
//        cal.add(Calendar.DATE, n*7);
////想周几，这里就传几Calendar.MONDAY（TUESDAY...）
//        cal.set(Calendar.DAY_OF_WEEK,Calendar.MONDAY);
//        monday = new SimpleDateFormat("yyyy-MM-dd").format(cal.getTime());
//        System.out.println(monday);













//        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd"); //设置时间格式
//        Calendar cal = Calendar.getInstance();
//        Date time= null;
//        try {
//            time = sdf.parse("2018-11-24");
//        } catch (ParseException e) {
//            e.printStackTrace();
//        }
//        cal.setTime(time);
//        System.out.println("要计算日期为:"+sdf.format(cal.getTime())); //输出要计算日期
//
//        //判断要计算的日期是否是周日，如果是则减一天计算周六的，否则会出问题，计算到下一周去了
//        int dayWeek = cal.get(Calendar.DAY_OF_WEEK);//获得当前日期是一个星期的第几天
//        if(1 == dayWeek) {
//            cal.add(Calendar.DAY_OF_MONTH, -1);
//        }
//
//        cal.setFirstDayOfWeek(Calendar.MONDAY);//设置一个星期的第一天，按中国的习惯一个星期的第一天是星期一
//
//        int day = cal.get(Calendar.DAY_OF_WEEK);//获得当前日期是一个星期的第几天
//        cal.add(Calendar.DATE, cal.getFirstDayOfWeek()-day);//根据日历的规则，给当前日期减去星期几与一个星期第一天的差值
//        System.out.println("所在周星期一的日期："+sdf.format(cal.getTime()));
//        System.out.println(cal.getFirstDayOfWeek()+"-"+day+"+6="+(cal.getFirstDayOfWeek()-day+6));
//
//        cal.add(Calendar.DATE, 6);
//        System.out.println("所在周星期日的日期："+sdf.format(cal.getTime()));

//    }
}
